function loss = myCVlossfcn_F1(params,In,Out,opts,cvp)

order = categorical([0; 1]);
A = @(XTRAIN,YTRAIN,XTEST,YTEST)(confusionmat(YTEST,...
                                                       categorical(...
                                                       cellfun(@str2num,... % convert cell array of character vectors to a cell array of numerics
                                                       predict(...
                                                        TreeBagger(...
                                                            300,XTRAIN,YTRAIN,...
                                                            'method','classification',...
                                                            'OOBPrediction','on',...
                                                            'Options',opts,...
                                                            'MinLeafSize',params.minLS,...
                                                            'NumPredictorsToSample', params.numPTS),...
                                                                XTEST...
                                                               ))),...
                                                       'order', order...
                                                      )...
                                          );
                                      
cfMat = crossval(A,In,Out,'partition',cvp);
cfMat = reshape(sum(cfMat),2,2); % summation of the 10 confusion matrices over the 10CV data sets
%Calculate recall, precision and F1
recall = cfMat(1)/(cfMat(1)+ cfMat(3));
precision = cfMat(1)/(cfMat(1) + cfMat(2));
F1 = 2*(precision * recall)/(precision + recall);
loss = 1 - F1;

end